package com.mapforge.selection;

import java.awt.Point;

/**
 * This class alters the size of the selection so that it is always the same
 * size no matter the original orientation of the shape.
 * 
 * @see com.mapforge.selection.SelectionModifier
 * @author Will Morrison
 * 
 */
public class SelectionFixedSizeModifier extends AbstractSelectionModifier {
	private int width;
	private int height;

	/**
	 * Constructs a new instance with 0 as a fixed width and height.
	 */
	public SelectionFixedSizeModifier() {
		this(0, 0);
	}

	/**
	 * Creates a new <code>SelectionFixedSizeModifier</code> with the specified
	 * fixed width and height.
	 * 
	 * @param width
	 *            specified fixed width
	 * @param height
	 *            specified fixed height
	 */
	public SelectionFixedSizeModifier(int width, int height) {
		this.width = width;
		this.height = height;
	}

	/**
	 * Sets this <code>SelectionFixedSizeModifier</code>'s fixed width and
	 * height.
	 * 
	 * @param width
	 *            specified fixed width
	 * @param height
	 *            specified fixed height
	 */
	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	/**
	 * Returns the fixed width dimension.
	 * 
	 * @return fixed width of this modifier
	 */
	public int getWidth() {
		return this.width;
	}

	/**
	 * Sets the fixed width of this modifier
	 * 
	 * @param width
	 *            new fixed width of this modifier
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * Sets the fixed height of this modifier
	 * 
	 * @param height
	 *            new fixed height of this modifier
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * Returns the fixed height of this modifier
	 * 
	 * @return fixed height of this modifier
	 */
	public int getHeight() {
		return this.height;
	}

	@Override
	public void modifyFromCenter(Point anchorPoint, Point movingPoint) {
		int dx = Math.abs(anchorPoint.x - movingPoint.x) / 2;
		int dy = Math.abs(anchorPoint.y - movingPoint.y) / 2;
		anchorPoint.translate(-dx, -dy);
		movingPoint.translate(-dx, -dy);
	}

	@Override
	public void doModify(Point startPoint, Point currentPoint) {
		startPoint.setLocation(currentPoint);
		currentPoint.translate(this.width, this.height);
	}
}
